package com.example.fw;

import static org.junit.Assert.fail;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebDriverHelper {

	public static WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private AppManager manager;

	public WebDriverHelper(AppManager manager) {
		this.manager = manager;
		driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.get("http://localhost/addressbookv4.1.4/");
	}

	public void stop() {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	public WebDriver getDriver() {
		return driver;
	}

	public boolean isElementPresent(By locator) {
		driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
		boolean verdict = driver.findElements(locator).size() > 0;
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		return verdict;
	}

}
